package com.springboot2.infrastructure.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot2.infrastructure.entity.TestObj;
import com.springboot2.infrastructure.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class ReidsController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/set")
    public boolean setValue(String key, String value) {
        redisService.setKey(key, value);
        return true;
    }

    @RequestMapping("/get")
    public String getValue(String key) {
        String value = redisService.getValue(key);
        return value;
    }

    @RequestMapping("/setObj")
    public boolean setObj() {
        redisService.setObj("obj",new TestObj());
        return true;
    }

    @RequestMapping("/getObj")
    public String getObj(String key) {
        TestObj testObj = (TestObj) redisService.getObj(key);
        return JSONObject.toJSONString(testObj);
    }

}

